package com.yonyou.crm.base.product.appctrl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.base.product.entity.ProductVO;
import com.yonyou.crm.base.product.rmitf.IProductRmService;
import com.yonyou.crm.bpub.list.entity.*;
import com.yonyou.crm.common.base.AppBaseCtrl;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.AppCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.common.sort.entity.AppSortPlanEnum;


@RestController
@RequestMapping(value = "/{version}/mbpub/products")
public class AppProductCtrl extends AppBaseCtrl{
	@Autowired
	private IProductRmService service;
	
	/**
	 * 产品列表
	 * @param version
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object getList(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		int page = paramObject.getInt("page");
		int pageSize = paramObject.getInt("pageSize");
		Page<ProductVO> pageInfo = new Page<ProductVO>(page, pageSize);
		Map<String, Object> searchMap = new HashMap<String, Object>();
		
		//搜索关键字
		getSearchKeyForList(paramObject, searchMap);
		//列表筛选
		getFilterForList(paramObject, searchMap);
		//列表数据分类
		getOptionForList(paramObject, searchMap);
		//排序依据
		getSortOrderForPage(paramObject, pageInfo);
			
		Page<ProductVO> productPage = service.page(pageInfo, searchMap);
		List<ProductVO> dataList = productPage.getData();
		List<AppListVO> list = new ArrayList<AppListVO>();
		if (dataList != null) {
			for (int i = 0; i<dataList.size(); i++) {
				ProductVO product = dataList.get(i);

				AppListVO listVO = new AppListVO();
				listVO.setId(product.getId());
				
				List<List<AppListValueVO>> valueList = new ArrayList<>();
    			List<AppListValueVO> colList = new ArrayList<>();
    			AppListValueVO listValue;

				listValue = new AppListValueVO();
				listValue.setItemkey("name");
				listValue.setDisplayvalue(product.getName());
				colList.add(listValue);
				
				valueList.add(colList);
    			colList = new ArrayList<>();
				
				listValue = new AppListValueVO();
				listValue.setItemkey("code");
				listValue.setDisplayvalue(product.getCode());
				colList.add(listValue);
				
				listValue = new AppListValueVO();
				listValue.setItemkey("price");
				listValue.setDisplayvalue("¥"+transformPrice(product.getPrice()));
				colList.add(listValue);
				
				listValue = new AppListValueVO();
				listValue.setItemkey("measure");
				String measureName = "";
				if ((product.getMeasureId() != null) && (product.getMeasureId() != 0)) {
					measureName = product.getMeasureName();
				}
				listValue.setDisplayvalue(measureName);
				colList.add(listValue);
				valueList.add(colList);

				listVO.setValuelist(valueList);
				list.add(listVO);
			}
		}
		
		Map<String, Object> productList = new HashMap<String,Object>();
		productList.put("list", list);
		return ResponseUtil.getObject(productList);
	}
	
	/**
	 * 排序依据
	 * @param paramObject
	 * @param pageInfo
	 * @throws CrmMessageException
	 */
	private void getSortOrderForPage(JSONObject paramObject, Page<ProductVO> pageInfo) throws CrmMessageException {
		Map<String, String> sortInfo = new HashMap<>();
		getSortForList(paramObject, sortInfo);
		String sortStr = "";
		String orderStr = "";
		if ((sortInfo != null) && (sortInfo.size() > 0)) {
			orderStr = String.valueOf(sortInfo.get("order"));
			Integer sortNum = Integer.valueOf(String.valueOf(sortInfo.get("field")));
			if (sortNum == AppSortPlanEnum.CODE.getValue()) {
				sortStr = "p.`code`";
			} else if (sortNum == AppSortPlanEnum.MODIFYTIME.getValue()) {
				sortStr = "p.sys_modified_time";
			} else {
				sortStr = "p.id";
				orderStr = "desc";
			}
		}
		pageInfo.setSort(sortStr);
		pageInfo.setOrder(orderStr);
	}
	
	/**
	 * 产品详情
	 * @param version
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value="/{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("version") String version, @PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		if (id == null) {
			throw new CrmMessageException(AppCodeConstant.ClientError, "产品id不能为空");
		}
		
		ProductVO product = service.detail(id);
		
		Map<String, Object> res = new HashMap<String, Object>();
		
		Map<String, Object> productDetail = new HashMap<String, Object>();
		
		String brandName = "";
		if ((product.getBrandId() != null) && (product.getBrandId() != 0)) {
			brandName = product.getBrandName();
		}
		productDetail.put("brandname", brandName);
		productDetail.put("name", product.getName());
		
		String prdtypeName = "";
		if ((product.getPrdtypeId() != null) && (product.getPrdtypeId() != 0)) {
			prdtypeName = product.getPrdtypeName();
		}
		productDetail.put("type", prdtypeName);
		productDetail.put("price", transformPrice(product.getPrice()));
		
		String measureName = "";
		if ((product.getMeasureId() != null) && (product.getMeasureId() != 0)) {
			measureName = product.getMeasureName();
		}
		productDetail.put("measure", measureName);
		ArrayList<Map<String, Object>> image = new ArrayList<Map<String, Object>>();
		Map<String, Object> pic = new HashMap<String, Object>();
		pic.put("imageurl", product.getPhoto());
		image.add(pic);
		productDetail.put("image", image);

		//productDetail.put("property", product.getProperty());
		//productDetail.put("saleunit", product.getSaleUnit());
		//TODO产品属性
		productDetail.put("property", new ArrayList<>());
		//TODO产品销售单位
		productDetail.put("saleunit", new ArrayList<>());
		productDetail.put("desc", product.getDescription());
		productDetail.put("followflag", product.getFollowFlag());

		res.put("productdetail", productDetail);
		return ResponseUtil.getObject(res);
	}
	
	/**
	 * 关注产品
	 * @param version
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value="/{id}/follow", method = RequestMethod.POST)
	public Object follow(@PathVariable("version") String version, @PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String followFlag = paramObject.getString("followState");
		
		if (id == null) {
			throw new CrmMessageException(AppCodeConstant.ClientError, "产品id不能为空");
		}
		
		if (followFlag.equals("N")) {
			service.deleteProductFollow(id);
		} else if (followFlag.equals("Y")) {
			service.insertProductFollow(id);
		} else {
			throw new CrmMessageException(AppCodeConstant.ClientError, "非法产品关注标识");
		}

		return ResponseUtil.getObject();
	}
	
}
